package edu.cth.speedappteachers;


import java.util.Scanner;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.LinearLayout;

public class InRoomActivity extends Activity {
	private int tempoOk = 0, 
				   tempoHard = 0, 
				   tempoEasy= 0, 
				   tempoStop= 0; 
	
	private LinearLayout rowStop, rowHard, rowOk, rowEasy;
	
	private String roomName, deviceUuid;
	private static final String baseUrl = "http://10.0.2.2/CIU196";
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_room);
        
        // Android device ID
        DeviceUuidFactory deviceUuidFactory = new DeviceUuidFactory(this);
        deviceUuid = deviceUuidFactory.getDeviceUuid().toString();
        
        // Room information
        Bundle extras = getIntent().getExtras();
    	
        if(extras != null ) {
        	roomName = extras.getString("roomName");
        }
    	
    	// Ask for the tempo distribution
    	new getTempoDistribution().execute(baseUrl + "/getTempoDistribution.php?roomID=" + roomName + "&&ownderID=" + deviceUuid);
    
        rowStop = (LinearLayout) findViewById(R.id.rowStop);
        rowHard = (LinearLayout) findViewById(R.id.rowHard);
        rowEasy = (LinearLayout) findViewById(R.id.rowEasy);
        rowOk = (LinearLayout) findViewById(R.id.rowOk);
	}
	
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_in_room, menu);
        return true;
    }
    
 	 /**
     * Creates a http request to see if anything is changed in the database
     * */
    class getTempoDistribution extends AsyncTask<String, String, String> {
    	  @Override
    	    protected String doInBackground(String... uri) {
    		  HttpRequest r = new HttpRequest(uri);
    		  return r.getResponse();
    	  }

    	  @Override
    	    protected void onPostExecute(String result) {
    	        super.onPostExecute(result);
    	        Log.d("speedappteacher", result);
    	        
    	        Scanner sc = new Scanner(result);
    	       
    	        // Order is important
    	        while(sc.hasNextInt()){
	    	        tempoStop = sc.nextInt();
	    	        tempoHard = sc.nextInt(); 
	    	        tempoOk = sc.nextInt();
	    	        tempoEasy = sc.nextInt();
    	        }
    	        // Updates the LinearView weights
    	        rowStop.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,   
    	                LayoutParams.WRAP_CONTENT,
    	                tempoStop+1));
    	        
    	        rowHard.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,   
    	                LayoutParams.WRAP_CONTENT,
    	                tempoHard+1));
    	        
    	        rowOk.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,   
    	                LayoutParams.WRAP_CONTENT,
    	                tempoOk+1));
    	        
    	        rowEasy.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,   
    	                LayoutParams.WRAP_CONTENT,
    	                tempoEasy+1));
    	    }
    }
}
